//
//  WNModel.m
//  Whatnet_iOS
//
//  Created by Bill on 2019/4/16.
//  Copyright © 2019 什么网络. All rights reserved.
//
#import "WNModel.h"
#import <objc/runtime.h>

@implementation WNModel

- (instancetype)initWithDict:(NSDictionary *)dict
{
    if (self = [super init]){
        
    }
    return self;
}
- (instancetype)initWithArray:(NSArray *)array
{
    if (self = [super init]){
        
    }
    return self;
}
//归解档相关
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super init];
    if (self) {
        if ([self respondsToSelector:@selector(codingKeys)]) {
            NSArray *keys = [self performSelector:@selector(codingKeys)];
            for (NSString *k in keys) {
                @try {
                    [self setValue:[aDecoder decodeObjectForKey:k] forKey:k];
                } @catch (NSException *exception) {
                    NSLog(@"%@",exception);
                }
            }
        }
    }
    return self;
}
//归解档相关
- (void)encodeWithCoder:(NSCoder *)aCoder {
    if ([self respondsToSelector:@selector(codingKeys)]) {
        NSArray *keys = [self performSelector:@selector(codingKeys)];
        for (NSString *k in keys) {
            @try {
                [aCoder encodeObject:[self valueForKey:k] forKey:k];
            } @catch (NSException *exception) {
                NSLog(@"encode failed:%@,property:%@",exception,k);
            }
        }
    }
}


/**
 字典转模型

 @param dict 字典
 @return 返回对应的模型
 */
+ (instancetype)modelWithDict:(NSDictionary *)dict
{
    if (![dict isKindOfClass:[NSDictionary class]]) {
        return nil;
    }
    id  objc = [[self alloc] init];
    unsigned int count = 0;
    Ivar *ivarList = class_copyIvarList(self, &count);
    for (int i = 0; i < count; i ++) {
        Ivar ivar = ivarList[i];
        NSString *ivarName = [NSString stringWithUTF8String:ivar_getName(ivar)];
        NSString *ivarType = [NSString stringWithUTF8String:ivar_getTypeEncoding(ivar)];
        ivarType = [ivarType stringByReplacingOccurrencesOfString:@"\"" withString:@""];
        ivarType = [ivarType stringByReplacingOccurrencesOfString:@"@" withString:@""];
        NSString *key = [ivarName substringFromIndex:1];
        
        if ([self respondsToSelector:@selector(propertyKeyDic)]) {
            id idSelf = self;
            key  = [idSelf propertyKeyDic][key]?:key;
        }
        id value = dict[key];
        if ([value isKindOfClass:[NSDictionary class]] && ![ivarType hasPrefix:@"NS"]) {
            Class modelClass = NSClassFromString(ivarType);
            if (modelClass) {
                value = [modelClass modelWithDict:value];
            }
        }
        if ([value isKindOfClass:[NSArray class]]) {
            if ([self respondsToSelector:@selector(arrayContainModelClass)]) {
                id idSelf = self;
                NSString *type = [idSelf arrayContainModelClass][key];
                Class classModel = NSClassFromString(type);
                if (classModel) {
                    NSMutableArray *arrM = [NSMutableArray new];
                    for (NSDictionary *dict in value) {
                        id model = [classModel modelWithDict:dict];
                        [arrM addObject:model];
                    }
                    value = arrM;
                }
            }
        }
        if (value) {
            [objc setValue:value forKey:[ivarName substringFromIndex:1]];
        }
    }
    return objc;
}
/**
 数组中保存的 对象字典
 
 @return  @{对象属性值：对应的model类名字}字典
 */
+ (NSDictionary *)arrayContainModelClass
{
    return @{};
}

/**
 字典和对象值不一致时 设置
 
 @return @{对象属性值：字典中存的值}
 */
+  (NSDictionary *)propertyKeyDic
{
    return @{@"ID":@"id"};
}
- (NSDictionary *)dictionary
{
    NSMutableDictionary *userDic = [NSMutableDictionary dictionary];
    unsigned int count = 0;
    objc_property_t *properties = class_copyPropertyList([self class], &count);
    for (int i = 0; i < count; i++) {
        const char *name = property_getName(properties[i]);
        NSString *propertyName = [NSString stringWithUTF8String:name];
        
        id propertyValue = [self valueForKey:propertyName];
        if (propertyValue && ![propertyName isEqualToString:@"ID"]) {
            [userDic setObject:propertyValue forKey:propertyName];
        }
    }
    free(properties);
    
    return userDic;
    
}

- (BOOL)checkComplete
{
    return YES;
}
@end

@implementation WNModelHelp

+ (NSMutableArray *)helpWithDefault
{
    return [NSMutableArray array];
}

+ (NSString *)titleWithSection:(NSInteger)section
{
    return nil;
}
+ (NSString *)titleWithIndexPath:(NSIndexPath *)indexPath
{
    return nil;
}

+ (instancetype)modelWithDict:(NSDictionary *)dict
{
    return [[self alloc] initWithDict:dict];
}

- (instancetype)initWithDict:(NSDictionary *)dict
{
    if (self = [super init]){
        
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super init]){
        
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{
    
}

@end

